# coding=utf-8
import os

import yaml
from Page.log import log


class Config:
    def __init__(self):
        self.log = log()
        # 读yaml配置文件
        resource_path = os.path.dirname(os.path.abspath(__file__))
        with open(resource_path+'/config.yaml', 'rb') as f:
            data = yaml.safe_load(f)
            self.environ = data['environ']

    def using_headless(self):
        """
        是否启用无头模式。using_headless两个来源，yaml文件和Jenkins添加的参数，先读Jenkins参数，不存在则读yaml文件的参数，都不存在则不启用无头模式。
        :return: bool
        """
        try:
            using_headless = os.environ["using_headless"]
            if using_headless is not None and using_headless.lower() == 'true':
                print('使用os.environ参数进行无界面方式运行')
                self.log.info('使用os.environ参数进行无界面方式运行')
                return True
        except KeyError:
            self.log.info("【os.environ】不存在using_headless参数！")
            try:
                using_headless = self.environ['using_headless']
                if using_headless is not None and (using_headless is True or using_headless.lower() == 'true'):
                    self.log.info('使用yaml参数进行无界面方式运行')
                    return True
            except KeyError:
                self.log.info("【./../Resource/config.yaml】不存在using_headless参数！")
                return False

    def conf(self, param):
        """
        传入参数key，获取参数值，具体参数查看../Resource/config.yaml文件
        :param param:
        :return:bool or string
        """
        try:
            value = self.environ[param]
            # config.yaml定义了参数，但是对应值为""，则不使用此参数
            if value != "":
                self.log.info(f'启用参数：{param}={value}执行测试！')
                return value
            else:
                return False
        except KeyError:
            self.log.info(f'参数：{param}不存在！')
            return False
